package com.abocode.aichat.server.modules.protocol.handler;

import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;
import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by franky.
 */
public class PresenceGetHandler implements ModuleHandler {
    @Override
    public Replies execute(ChannelWrapper channelWrapper, Stanza stanza, ServerRuntimeContext serverRuntimeContext) {
        Map body = stanza.body();
        String jid = String.valueOf(body.get("jid"));
        Entity entity = EntityImpl.parse(jid);
        boolean flag =serverRuntimeContext.getProtocolChannelRegistry().isOnline(entity);
        Replies replies = null;
        if (flag) {
            replies = new Replies();
            Map map = new HashMap();
            map.put("presence", entity.getPresence());
            map.put("jid", entity.getFullName());
            replies.addReply(channelWrapper.getChannel(), StanzaWrapper.wrapper(stanza, map));
        }else{

        }
        return replies;
    }

    @Override
    public String module() {
        return "get";
    }
}
